import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;

public class LoanApplet extends JApplet
{
	private JPanel panel;
	private JPanel pan;
	private JPanel pan1;
	private JPanel pan2;
	private JPanel pan3;
	private JPanel pan4;
	private JPanel pan5;
		
	private JLabel mess1;
	private JLabel mess2;
	private JLabel mess3;
	private JLabel mess4;
	private JLabel mess5;
	
	private JTextField annualInterestRate;
	private JTextField numYears;
	private JTextField loanAmount;
	private JTextField monthlyPayment;
	private JTextField totalPayment;
	private JButton calc;
	
	private DecimalFormat fmt = new DecimalFormat("$#,###.00");
	
	public void init()
	{
		
		setLayout(new BorderLayout());
		setSize(350,200);
		
		buildPanel();
		buildPan5();
		
		panel.list();
		add(panel, BorderLayout.CENTER);
		add(pan5, BorderLayout.SOUTH);
	}
		 
	private void buildPanel()
	{
		
		
		panel = new JPanel();
		panel.setBorder(BorderFactory.createTitledBorder("Enter interest rate, year and loan amount"));
		
		mess1 = new JLabel("Annual Interest Rate");
		annualInterestRate = new JTextField(10);
		annualInterestRate.setHorizontalAlignment(JTextField.RIGHT);
		
		mess2 = new JLabel("Number of Years");
		numYears = new JTextField(10);
		numYears.setHorizontalAlignment(JTextField.RIGHT);
		
		mess3 = new JLabel("Loan Amount");
		loanAmount = new JTextField(10);
		loanAmount.setHorizontalAlignment(JTextField.RIGHT);
		
		mess4 = new JLabel("Monthly Payment");
		monthlyPayment = new JTextField(10);
		monthlyPayment.setEditable(false);
		monthlyPayment.setHorizontalAlignment(JTextField.RIGHT);
		
		mess5 = new JLabel("Total Payment");
		totalPayment = new JTextField(10);
		totalPayment.setEditable(false);
		totalPayment.setHorizontalAlignment(JTextField.RIGHT);
		
		panel.setLayout(new GridLayout(5,2));
		
		panel.add(mess1);
		panel.add(annualInterestRate);
		panel.add(mess2);
		panel.add(numYears);
		panel.add(mess3);
		panel.add(loanAmount);
		panel.add(mess4);
		panel.add(monthlyPayment);
		panel.add(mess5);
		panel.add(totalPayment);
	}


	private void buildPan5()
	{
		
				
		calc = new JButton("Compute Payment");
		calc.addActionListener(new convertButtonListener());
		
		pan5 = new JPanel();
		pan5.setLayout(new FlowLayout(FlowLayout.RIGHT));
		pan5.add(calc);
	}
	
	
	private class convertButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{
			String text, text1, text2; 
			Double monthlyPay;
			Double totalPay;
			text = annualInterestRate.getText();
			text1 = numYears.getText();
			text2 = loanAmount.getText();
			Double months = 12.0;
			
			Double p = Double.parseDouble(text2);
			Double i = Double.parseDouble(text);
			Double t = Double.parseDouble(text1);
			
			
			
			
						
			monthlyPay  = p*i/(months*100)/(1- Math.pow(i/(months*100)+1, -(t/1)*months)); 	
			totalPay = (monthlyPay * months) * t;
		  
			monthlyPayment.setText(fmt.format(monthlyPay).toString());
		    totalPayment.setText(fmt.format(totalPay).toString());
		}
	}
}


